﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class TotalSaleNumEchartController : BaseController<usersorder>
    {
        // 第一个图   总营业额走势图
        // GET: /TotalSaleNumEchart/

        protected admin CurrentUer
        {
            get { return Session["CurrentUer"] == null ? null : (admin)Session["CurrentUer"]; }
        }
        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }

        }

        public class OrderModel
        {
            /// <summary>
            /// 横坐标日期ss
            /// </summary>
            public string Date { get; set; }

            /// <summary>
            /// 总销量
            /// </summary>
            public string CountMoney { get; set; }
        }


        UserBll userbll = new UserBll();

        /// <summary>
        /// 销售额 统计图
        /// </summary>
        /// <param name="date1"></param>
        /// <param name="date2"></param>
        /// <returns></returns>

        public ActionResult GetOrderAll(string date1, string date2, string storeid)
        {          
            List<OrderModel> dataList = new List<OrderModel>();
            Expression<Func<usersorder, bool>> condition = null;
            //日期查询  先查找出满足时间的条件   默认查询近一周的所有店铺的记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                condition = x => x.orderStatus == 1 && x.orderTime <= DateTime.Now;
                var query = bll.Search(condition).ToList();
                //先算6 天的   
                for (var i = 0; i <= 6; i++)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;
                    DateTime datee = DateTime.Now.Date.AddDays(-countData);
                    //一天的数据
                    var queryable = query.Where(u => u.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && u.orderStatus == 1);
                    var dd = queryable.ToList();
                    decimal? sellIncome = 0;
                    var sellIncomeList = (from r in queryable

                                          select r.orderSumprice + r.discountMoney).ToList();
                    foreach (var oDecimal in sellIncomeList)
                    {
                        sellIncome += oDecimal;
                    }
                    orderModel.Date = DateTime.Now.Date.AddDays(-countData).ToString("yy/MM/dd");
                    orderModel.CountMoney = sellIncome.ToString();
                    dataList.Add(orderModel);
                }
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            //店铺查询查询   默认查询近一周的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != null && storeid != "")
            {
                int storeID = Convert.ToInt32(storeid);
                condition = x => x.orderStatus == 1 && x.orderTime <= DateTime.Now && x.storeid == storeID;
                var query = bll.Search(condition).ToList();
                //先算6 天的   
                for (var i = 0; i <= 6; i++)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;
                    DateTime datee = DateTime.Now.Date.AddDays(-countData);
                    //一天的数据
                    var queryable = query.Where(u => u.orderTime.Date == DateTime.Now.Date.AddDays(-countData) && u.orderStatus == 1);
                    var dd = queryable.ToList();
                    decimal? sellIncome = 0;
                    var sellIncomeList = (from r in queryable

                                          select r.orderSumprice + r.discountMoney).ToList();
                    foreach (var oDecimal in sellIncomeList)
                    {
                        sellIncome += oDecimal;
                    }
                    orderModel.Date = DateTime.Now.Date.AddDays(-countData).ToString("yy/MM/dd");
                    orderModel.CountMoney = sellIncome.ToString();
                    dataList.Add(orderModel);//把这里忘记了
                }
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            //根据日期查询所有的
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && string.IsNullOrEmpty(storeid))
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                TimeSpan sp = datetime2.Subtract(datetime1);  //这里验证通过  不在一个月的日期也可以
                Expression<Func<usersorder, bool>> condition1 = null;
                //相差天数
                int dayMin = sp.Days;
                //先查出订单时间是统计时间段的订单记录
                condition1 = x => x.orderTime >= datetime1 && x.orderTime < datetime2 && x.orderStatus == 1;
                var list = bll.Search().Where(condition1).Select(x => new { x.orderSumprice, x.orderTime, x.discountMoney }).ToList();
                //再查这几天的 循环
                for (var i = 0; i < dayMin; i++)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;

                    DateTime date11 = datetime1.Date.AddDays(+countData);

                    var queryable = list.Where(u => u.orderTime.Date == date11.Date);//就
                    var dd = queryable.ToList();

                    decimal? sellIncome = 0;
                    var sellIncomeList = (from r in queryable

                                          select r.orderSumprice + r.discountMoney).ToList();
                    foreach (var oDecimal in sellIncomeList)
                    {
                        sellIncome += oDecimal;
                    }
                    orderModel.Date = date11.Date.ToString("yy/MM/dd");
                    orderModel.CountMoney = sellIncome.ToString();
                    dataList.Add(orderModel);
                }
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }
            //根据日期查询 和  店铺查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != null && storeid != "")
            {
                int storeID=Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                TimeSpan sp = datetime2.Subtract(datetime1);  //这里验证通过  不在一个月的日期也可以
                Expression<Func<usersorder, bool>> condition1 = null;
                //相差天数
                int dayMin = sp.Days;
                //先查出订单时间是统计时间段的订单记录
                condition1 = x => x.orderTime >= datetime1 && x.orderTime < datetime2 && x.orderStatus == 1 && x.storeid == storeID;
                var list = bll.Search().Where(condition1).Select(x => new { x.orderSumprice, x.orderTime, x.discountMoney }).ToList();
                //再查这几天的 循环
                for (var i = 0; i < dayMin; i++)
                {
                    OrderModel orderModel = new OrderModel();
                    var countData = i;

                    DateTime date11 = datetime1.Date.AddDays(+countData);

                    var queryable = list.Where(u => u.orderTime.Date == date11.Date);//就
                    var dd = queryable.ToList();

                    decimal? sellIncome = 0;
                    var sellIncomeList = (from r in queryable

                                          select r.orderSumprice + r.discountMoney).ToList();
                    foreach (var oDecimal in sellIncomeList)
                    {
                        sellIncome += oDecimal;
                    }
                    orderModel.Date = date11.Date.ToString("yy/MM/dd");
                    orderModel.CountMoney = sellIncome.ToString();
                    dataList.Add(orderModel);
                }
                var dataArray = dataList.ToArray();
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }
             return Json(dataList, JsonRequestBehavior.AllowGet);
        }


        public ActionResult GetMoneyAll(string date1, string date2, string storeid)
        {
            List<OrderModel> dataList = new List<OrderModel>();
            //日期查询  先查找出满足时间的条件   默认查询近一周的所有店铺的记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                DateTime datenow = DateTime.Now;
                DateTime datestart= DateTime.Now.AddDays(-6).Date;
                string sql = "select orderTime as Date,sum(discountMoney+orderSumprice) as CountMoney from usersorder  FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + datestart + "'  and '" + datenow + "'  GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<OrderModel>(sql).ToList();
                foreach (var item in list)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.Date = Convert.ToDateTime(item.Date).ToString("MM/dd");
                    orderModel.CountMoney = item.CountMoney;
                    dataList.Add(orderModel);
                }             
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }
            //店铺查询查询   默认查询近一周的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != null && storeid != "")
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime datenow = DateTime.Now;
                DateTime datestart = DateTime.Now.AddDays(-6).Date;
                string sql = "select orderTime as Date,sum(discountMoney+orderSumprice) as CountMoney from usersorder FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + datestart + "'  and '" + datenow + "'  and storeid=" + storeID + "  GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<OrderModel>(sql).ToList();
                foreach (var item in list)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.Date = Convert.ToDateTime(item.Date).ToString("MM/dd");
                    orderModel.CountMoney = item.CountMoney;
                    dataList.Add(orderModel);
                }
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            //根据日期查询所有的
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && string.IsNullOrEmpty(storeid))
            {
                DateTime datestart = DateTime.Parse(date1);
                DateTime datenow = DateTime.Parse(date2).AddDays(1);
                string sql = "select orderTime as Date,sum(discountMoney+orderSumprice) as CountMoney from usersorder FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + datestart + "'  and '" + datenow + "'  GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<OrderModel>(sql).ToList();
                foreach (var item in list)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.Date = Convert.ToDateTime(item.Date).ToString("MM/dd");
                    orderModel.CountMoney = item.CountMoney;
                    dataList.Add(orderModel);
                }
                return Json(dataList, JsonRequestBehavior.AllowGet);

            }
            //根据日期查询 和  店铺查询
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != null && storeid != "")
            {
                int storeID = Convert.ToInt32(storeid);
                DateTime datestart = DateTime.Parse(date1);
                DateTime datenow = DateTime.Parse(date2).AddDays(1);

                string sql = "select orderTime as Date,sum(discountMoney+orderSumprice) as CountMoney from usersorder FORCE index(idx_userorder_orderTime) where orderStatus=1 and orderTime BETWEEN '" + datestart + "'  and '" + datenow + "'  and storeid=" + storeID + "  GROUP BY DATE(orderTime)";
                var list = bll.SearchBySQL<OrderModel>(sql).ToList();
                foreach (var item in list)
                {
                    OrderModel orderModel = new OrderModel();
                    orderModel.Date = Convert.ToDateTime(item.Date).ToString("MM/dd");
                    orderModel.CountMoney = item.CountMoney;
                    dataList.Add(orderModel);
                }
                return Json(dataList, JsonRequestBehavior.AllowGet);
            }
            return Json(dataList, JsonRequestBehavior.AllowGet);
        }
    }
    }

